﻿namespace WMS6.Web.Inbound
{
    using NPOI.HPSF;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.CommonMethodService2Ref;

    public class ReceiptInvPallet : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnAudit;
        protected Button btnDeleteDetail;
        protected Button btnExport;
        protected Button btnImport;
        protected Button btnRevertAudit;
        protected Button btnSave;
        protected FileUpload fileupPallet;
        protected GridView gvInvPallet;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnExecDateRowIndex;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnOrderID;
        protected HtmlInputHidden hdnOwnerID;
        protected HtmlInputHidden hdnRowIndex;
        protected HtmlHead Head1;
        protected Label lblInsert;
        protected Label lblMsg;
        protected Label lblReceiptInvPallet;
        protected Label lblRed;
        protected Label lblRedText;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            DataTable invPalletDetailDT = this.GetInvPalletSchema();
            if (this.gvInvPallet.Rows.Count > 0)
            {
                invPalletDetailDT.Rows.Clear();
                for (int i = 0; i < this.gvInvPallet.Rows.Count; i++)
                {
                    if (this.gvInvPallet.Rows[i].RowType == DataControlRowType.DataRow)
                    {
                        DataRow tempdr = invPalletDetailDT.NewRow();
                        tempdr["SkuID"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim();
                        tempdr["Case"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtCase")).Text.Trim();
                        tempdr["Pallet"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtPallet")).Text.Trim();
                        tempdr["ExecDate"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtExecDate")).Text.Trim();
                        tempdr["Remark"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtRemark")).Text.Trim();
                        invPalletDetailDT.Rows.Add(tempdr);
                    }
                }
            }
            DataRow dr = invPalletDetailDT.NewRow();
            dr["ExecDate"] = ((TextBox) this.gvInvPallet.Rows[this.gvInvPallet.Rows.Count - 1].FindControl("txtExecDate")).Text.Trim();
            invPalletDetailDT.Rows.Add(dr);
            base.BindData2GridView(this.gvInvPallet, invPalletDetailDT);
            ((TextBox) this.gvInvPallet.Rows[this.gvInvPallet.Rows.Count - 1].FindControl("txtSkuID")).Focus();
        }

        protected void btnAudit_Click(object sender, EventArgs e)
        {
            try
            {
                if (CommoneMethodService2ClientProxy.GetInstance().UpdateASNAndSOSingleField("ASN", this.hdnOrderID.Value, "IS_PALLET_AUDIT", "Y") > 0)
                {
                    this.lblMsg.Text = "审核成功!";
                    this.btnAddDetail.Enabled = false;
                    this.btnDeleteDetail.Enabled = false;
                    this.btnSave.Enabled = false;
                    this.btnImport.Enabled = false;
                    this.btnExport.Enabled = false;
                    this.btnAudit.Enabled = false;
                    this.fileupPallet.Enabled = false;
                    this.btnRevertAudit.Enabled = true;
                }
                else
                {
                    this.lblMsg.Text = "审核失败!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            DataRow dr;
            DataTable invPalletDetailDT = this.GetInvPalletSchema();
            if (this.gvInvPallet.Rows.Count > 0)
            {
                invPalletDetailDT.Rows.Clear();
                for (int i = 0; i < this.gvInvPallet.Rows.Count; i++)
                {
                    if ((this.gvInvPallet.Rows[i].RowType == DataControlRowType.DataRow) && !((CheckBox) this.gvInvPallet.Rows[i].FindControl("chbSelect")).Checked)
                    {
                        DataRow tempdr = invPalletDetailDT.NewRow();
                        tempdr["SkuID"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim();
                        tempdr["Case"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtCase")).Text.Trim();
                        tempdr["Pallet"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtPallet")).Text.Trim();
                        tempdr["ExecDate"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtExecDate")).Text.Trim();
                        tempdr["Remark"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtRemark")).Text.Trim();
                        invPalletDetailDT.Rows.Add(tempdr);
                    }
                }
            }
            if (invPalletDetailDT == null)
            {
                invPalletDetailDT = this.GetInvPalletSchema();
                dr = invPalletDetailDT.NewRow();
                invPalletDetailDT.Rows.Add(dr);
            }
            else if (invPalletDetailDT.Rows.Count == 0)
            {
                dr = invPalletDetailDT.NewRow();
                invPalletDetailDT.Rows.Add(dr);
            }
            base.BindData2GridView(this.gvInvPallet, invPalletDetailDT);
        }

        public void btnExport_Click(object sender, EventArgs e)
        {
            if (this.gvInvPallet.Rows.Count > 0)
            {
                string fileName = "InboundPallet.xls";
                this.gvInvPallet.Columns[0].Visible = false;
                this.gvInvPallet.Columns[1].Visible = false;
                string filename = HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString();
                base.Response.ContentType = "application/vnd.ms-excel";
                base.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString()));
                base.Response.Clear();
                HSSFWorkbook hssfworkbook = new HSSFWorkbook();
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "QuantumAsia";
                hssfworkbook.DocumentSummaryInformation = dsi;
                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                si.Subject = "Export Data To Excel";
                hssfworkbook.SummaryInformation = si;
                Sheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
                Row excelRow = sheet1.CreateRow(0);
                excelRow.CreateCell(0).SetCellValue(this.gvInvPallet.Columns[2].HeaderText);
                excelRow.CreateCell(1).SetCellValue(this.gvInvPallet.Columns[3].HeaderText);
                excelRow.CreateCell(2).SetCellValue(this.gvInvPallet.Columns[4].HeaderText);
                excelRow.CreateCell(3).SetCellValue(this.gvInvPallet.Columns[5].HeaderText);
                excelRow.CreateCell(4).SetCellValue(this.gvInvPallet.Columns[6].HeaderText);
                for (int i = 0; i < this.gvInvPallet.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim()))
                    {
                        Row excelRow2 = sheet1.CreateRow(i + 1);
                        excelRow2.CreateCell(0).SetCellValue(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim());
                        excelRow2.CreateCell(1).SetCellValue(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtPallet")).Text.Trim());
                        excelRow2.CreateCell(2).SetCellValue(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtCase")).Text.Trim());
                        excelRow2.CreateCell(3).SetCellValue(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtExecDate")).Text.Trim());
                        excelRow2.CreateCell(4).SetCellValue(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtRemark")).Text.Trim());
                    }
                }
                MemoryStream file = new MemoryStream();
                hssfworkbook.Write(file);
                base.Response.BinaryWrite(file.GetBuffer());
                base.Response.End();
            }
        }

        protected void btnImport_Click(object sender, EventArgs e)
        {
            if ((this.fileupPallet.FileContent != null) && !string.IsNullOrEmpty(this.fileupPallet.FileName))
            {
                int i;
                DataTable invPalletDetailDT = this.GetInvPalletSchema();
                if (this.gvInvPallet.Rows.Count > 0)
                {
                    invPalletDetailDT.Rows.Clear();
                    for (i = 0; i < this.gvInvPallet.Rows.Count; i++)
                    {
                        if (this.gvInvPallet.Rows[i].RowType == DataControlRowType.DataRow)
                        {
                            DataRow tempdr = invPalletDetailDT.NewRow();
                            tempdr["SkuID"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim();
                            tempdr["Case"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtCase")).Text.Trim();
                            tempdr["Pallet"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtPallet")).Text.Trim();
                            tempdr["ExecDate"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtExecDate")).Text.Trim();
                            tempdr["Remark"] = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtRemark")).Text.Trim();
                            invPalletDetailDT.Rows.Add(tempdr);
                        }
                    }
                }
                HSSFWorkbook workbook = new HSSFWorkbook(this.fileupPallet.FileContent);
                HSSFSheet sheet = (HSSFSheet) workbook.GetSheetAt(0);
                HSSFRow headerRow = (HSSFRow) sheet.GetRow(0);
                for (i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
                {
                    DataRow dr = invPalletDetailDT.NewRow();
                    HSSFRow row = (HSSFRow) sheet.GetRow(i);
                    if (((row == null) || (row.GetCell(0) == null)) || (row.GetCell(0).ToString().Trim() == ""))
                    {
                        break;
                    }
                    for (int j = 0; j < headerRow.Cells.Count; j++)
                    {
                        string CS40001 = j.ToString();
                        if (CS40001 != null)
                        {
                            if (!(CS40001 == "0"))
                            {
                                if (CS40001 == "1")
                                {
                                    goto Label_0323;
                                }
                                if (CS40001 == "2")
                                {
                                    goto Label_0370;
                                }
                                if (CS40001 == "3")
                                {
                                    goto Label_03BD;
                                }
                                if (CS40001 == "4")
                                {
                                    goto Label_0407;
                                }
                            }
                            else if (row.GetCell(j) == null)
                            {
                                dr["SkuID"] = "";
                            }
                            else
                            {
                                dr["SkuID"] = row.GetCell(j).ToString();
                            }
                        }
                        continue;
                    Label_0323:
                        if (row.GetCell(j) == null)
                        {
                            dr["Pallet"] = "";
                        }
                        else
                        {
                            dr["Pallet"] = row.GetCell(j).ToString();
                        }
                        continue;
                    Label_0370:
                        if (row.GetCell(j) == null)
                        {
                            dr["Case"] = "";
                        }
                        else
                        {
                            dr["Case"] = row.GetCell(j).ToString();
                        }
                        continue;
                    Label_03BD:
                        if (row.GetCell(j) == null)
                        {
                            dr["ExecDate"] = "";
                        }
                        else
                        {
                            dr["ExecDate"] = row.GetCell(j).ToString();
                        }
                        continue;
                    Label_0407:
                        if (row.GetCell(j) == null)
                        {
                            dr["Remark"] = "";
                        }
                        else
                        {
                            dr["Remark"] = row.GetCell(j).ToString();
                        }
                    }
                    invPalletDetailDT.Rows.Add(dr);
                }
                base.BindData2GridView(this.gvInvPallet, invPalletDetailDT);
            }
        }

        protected void btnRevertAudit_Click(object sender, EventArgs e)
        {
            try
            {
                if (CommoneMethodService2ClientProxy.GetInstance().UpdateASNAndSOSingleField("ASN", this.hdnOrderID.Value, "IS_PALLET_AUDIT", "N") > 0)
                {
                    this.lblMsg.Text = "反审核成功!";
                    this.btnAddDetail.Enabled = true;
                    this.btnDeleteDetail.Enabled = true;
                    this.btnSave.Enabled = true;
                    this.btnImport.Enabled = true;
                    this.btnExport.Enabled = true;
                    this.btnAudit.Enabled = true;
                    this.fileupPallet.Enabled = true;
                    this.btnRevertAudit.Enabled = false;
                }
                else
                {
                    this.lblMsg.Text = "反审核失败!";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                List<InvPalletInfo> invPalletListInfo = new List<InvPalletInfo>();
                for (int i = 0; i < this.gvInvPallet.Rows.Count; i++)
                {
                    if (!string.IsNullOrEmpty(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim()))
                    {
                        InvPalletInfo invPalletInfo = new InvPalletInfo {
                            LineID = i + 1,
                            DocID = this.hdnOrderID.Value,
                            OwnerID = this.hdnOwnerID.Value,
                            DocType = "ASN",
                            SkuID = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtSkuID")).Text.Trim()
                        };
                        if (!string.IsNullOrEmpty(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtCase")).Text.Trim()))
                        {
                            invPalletInfo.Case = decimal.Parse(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtCase")).Text.Trim());
                        }
                        else
                        {
                            invPalletInfo.Case = 0M;
                        }
                        if (!string.IsNullOrEmpty(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtPallet")).Text.Trim()))
                        {
                            invPalletInfo.Pallet = decimal.Parse(((TextBox) this.gvInvPallet.Rows[i].FindControl("txtPallet")).Text.Trim());
                        }
                        else
                        {
                            invPalletInfo.Pallet = 0M;
                        }
                        invPalletInfo.ExecDate = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtExecDate")).Text.Trim();
                        if (string.IsNullOrEmpty(invPalletInfo.ExecDate))
                        {
                            this.lblMsg.Text = "请输入入库日期";
                            ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtExecDate")).Focus();
                            return;
                        }
                        invPalletInfo.Remark = ((TextBox) this.gvInvPallet.Rows[i].FindControl("txtRemark")).Text.Trim();
                        invPalletInfo.BillingCase = 0M;
                        invPalletInfo.BillingPallet = 0M;
                        invPalletInfo.CreatedBy = base.UserInfo.UserID;
                        invPalletInfo.UpdatedBy = base.UserInfo.UserID;
                        invPalletListInfo.Add(invPalletInfo);
                    }
                }
                if (invPalletListInfo.Count > 0)
                {
                    if (string.IsNullOrEmpty(CommoneMethodService2ClientProxy.GetInstance().InsertInvPallet(invPalletListInfo)))
                    {
                        this.lblMsg.Text = "箱托录入保存成功！";
                    }
                    else
                    {
                        this.lblMsg.Text = "箱托录入保存失败！";
                    }
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private DataTable GetInvPalletSchema()
        {
            DataTable invPalletDT = new DataTable();
            invPalletDT.Columns.Add("LineID");
            invPalletDT.Columns.Add("SkuID");
            invPalletDT.Columns.Add("Case");
            invPalletDT.Columns.Add("Pallet");
            invPalletDT.Columns.Add("ExecDate");
            invPalletDT.Columns.Add("Remark");
            return invPalletDT;
        }

        protected void gvInvPallet_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
                ((TextBox) e.Row.FindControl("txtSkuID")).Attributes.Add("onblur", "setSkuIDRowIndex(" + e.Row.RowIndex + ")");
                ((TextBox) e.Row.FindControl("txtExecDate")).Attributes.Add("onblur", "setExecDateRowIndex(" + e.Row.RowIndex + ")");
                if (string.IsNullOrEmpty(((TextBox) e.Row.FindControl("txtSkuID")).Text.Trim()))
                {
                    ((TextBox) e.Row.FindControl("txtSkuID")).BackColor = Color.Red;
                }
            }
        }

        public static bool IsDate(string value)
        {
            return Regex.IsMatch(value, @"^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-9]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$");
        }

        private bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"^[0-9]\d*$");
        }

        private void LoadData()
        {
            if (!string.IsNullOrEmpty(base.Request.QueryString["DOC_ID"]))
            {
                this.hdnOrderID.Value = base.Request.QueryString["DOC_ID"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["OwnerID"]))
            {
                this.hdnOwnerID.Value = base.Request.QueryString["OwnerID"].ToString();
            }
            List<InvPalletInfo> invPalletListInfo = CommoneMethodService2ClientProxy.GetInstance().GetInvPalletByIDAndType(this.hdnOrderID.Value, "ASN");
            base.BindData2GridView(this.gvInvPallet, invPalletListInfo);
            if (invPalletListInfo.Count == 0)
            {
                this.btnAddDetail.Enabled = false;
                this.btnDeleteDetail.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnImport.Enabled = false;
                this.btnExport.Enabled = false;
                this.btnAudit.Enabled = false;
                this.fileupPallet.Enabled = false;
                this.btnRevertAudit.Enabled = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["PA"]) && (base.Request.QueryString["PA"].ToUpper() == "TRUE"))
            {
                this.btnAddDetail.Enabled = false;
                this.btnDeleteDetail.Enabled = false;
                this.btnSave.Enabled = false;
                this.btnImport.Enabled = false;
                this.btnExport.Enabled = false;
                this.btnAudit.Enabled = false;
                this.fileupPallet.Enabled = false;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
            this.lblMsg.Text = "";
        }

        protected void txtExecDate_TextChanged(object sender, EventArgs e)
        {
            int rowIndex = 0;
            if (!string.IsNullOrEmpty(this.hdnExecDateRowIndex.Value))
            {
                rowIndex = int.Parse(this.hdnExecDateRowIndex.Value);
                string value = ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtExecDate")).Text;
                if (!string.IsNullOrEmpty(value) && !IsDate(value))
                {
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtExecDate")).Text = "";
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtExecDate")).Focus();
                }
            }
        }

        protected void txtSkuID_TextChanged(object sender, EventArgs e)
        {
            int rowIndex = 0;
            if (!string.IsNullOrEmpty(this.hdnRowIndex.Value))
            {
                rowIndex = int.Parse(this.hdnRowIndex.Value);
                string skuID = ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtSkuID")).Text.Trim();
                SkuInfo skuInfo = BaseInfoDetailServiceClientProxy.GetInstance().GetDetailInfoByID(this.hdnOwnerID.Value.ToString(), skuID, "", "SKU") as SkuInfo;
                if (skuInfo == null)
                {
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.Red;
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtSkuID")).Text = "";
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtSkuID")).Focus();
                }
                else
                {
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtSkuID")).BackColor = Color.White;
                    ((TextBox) this.gvInvPallet.Rows[rowIndex].FindControl("txtPallet")).Focus();
                }
            }
        }
    }
}

